package com.yuncheng.spcyApi.spcyService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.bo.JcydjbSetGzdwParamBo;
import com.yuncheng.spcyApi.bo.ParamsBo;
import com.yuncheng.spcyApi.config.SpcyMinioUtils;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import com.yuncheng.spcyApi.constant.common.ClientJumpUrlConstant;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.constant.jcy.JcydjbConstant;
import com.yuncheng.spcyApi.constant.sys.SysConfigConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.vo.HttpResult;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import utils.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

@Component(value = "spcyJcydjbService")
public class SpcyJcydjbService {

    private static final Logger log = LoggerFactory.getLogger(SpcyJcydjbService.class);

    @Resource
    private ISpcyRsJcydjbService spcyRsJcydjbService;

    @Resource
    private ISpcyRsJcydjlbService spcyRsJcydjlbService;

    @Resource
    private ISpcyRsJcydjgzjlService spcyRsJcydjgzjlService;

    @Resource
    private ISpcyRsJcydjfbwzService spcyRsJcydjfbwzService;

    @Resource
    private ISysConfigService sysConfigService;

    @Resource
    private BusinessConstant businessConstant;

    @Resource
    private WordPdfUtils wordPdfUtils;

    @Resource
    private LocalFileUploadUtils localFileUploadUtils;

    @Resource
    private SpcyMinioUtils spcyMinioUtils;

    @Resource
    private SmsUtils smsUtils;

    @Resource
    @Lazy
    private ISysUserGzjlService sysUserGzjlService;

    @Resource
    @Lazy
    private ISysUserFbwzService sysUserFbwzService;

    @Resource
    @Lazy
    private ISpcySmsLogService spcySmsLogService;

    @Resource
    @Lazy
    private ISpcyRsDwkService spcyRsDwkService;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    public String getJctcWithLb(String lb){

        if (lb.equals(JcydjbConstant.LB_YPLC)){
            return Optional.ofNullable(this.getSysConfigByCode(JcydjbConstant.SYS_CONFIG_JCYTC_YWLC_JCTC)).orElseGet(() -> "");
        }
        else if (lb.equals(JcydjbConstant.LB_YPZCSC)){
            return Optional.ofNullable(this.getSysConfigByCode(JcydjbConstant.SYS_CONFIG_JCYTC_YPZCSC_JCTC)).orElseGet(() -> "");
        }
        else if (lb.equals(JcydjbConstant.LB_YPLT)){
            return Optional.ofNullable(this.getSysConfigByCode(JcydjbConstant.SYS_CONFIG_JCYTC_YPLT_JCTC)).orElseGet(() -> "");
        }
        else if (lb.equals(JcydjbConstant.LB_YLQXSCLT)){
            return Optional.ofNullable(this.getSysConfigByCode(JcydjbConstant.SYS_CONFIG_JCYTC_YLQXSCLT_JCTC)).orElseGet(() -> "");
        }
        else if (lb.equals(JcydjbConstant.LB_YLQXLC)){
            return Optional.ofNullable(this.getSysConfigByCode(JcydjbConstant.SYS_CONFIG_JCYTC_YLQXLC_JCTC)).orElseGet(() -> "");
        }
        else if (lb.equals(JcydjbConstant.LB_HZPJCY)){
            return Optional.ofNullable(this.getSysConfigByCode(JcydjbConstant.SYS_CONFIG_JCYTC_HZPJCY_JCTC)).orElseGet(() -> "");
        }

        return Optional.ofNullable(this.getSysConfigByCode(JcydjbConstant.SYS_CONFIG_JCYTC_YPZCSC_JCTC)).orElseGet(() -> "");
    }

    public String getSysConfigByCode(String code){
        String msg = null;

        List<SysConfig> config = sysConfigService.list(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getConfigCode, code));

        if (CollectionUtils.isNotEmpty(config)){
            msg = config.get(0).getConfigValue();
        }

        return msg;
    }
    /**
     * 分页查询-登记表
     * @param jcydjb
     * @param pageNo
     * @param pageSize
     * @return
     */
    public HttpResult queryPageDjb(SpcyRsJcydjb jcydjb, String year, Integer pageNo, Integer pageSize){
        Page<SpcyRsJcydjb> page = new Page<>(pageNo, pageSize);

        LambdaQueryWrapper<SpcyRsJcydjb> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(jcydjb.getFsfzh()),SpcyRsJcydjb::getFsfzh,jcydjb.getFsfzh());
        queryWrapper.like(StringUtils.isNotBlank(jcydjb.getRealname()),SpcyRsJcydjb::getRealname,jcydjb.getRealname());
        queryWrapper.like(StringUtils.isNotBlank(jcydjb.getFgzdw()),SpcyRsJcydjb::getFgzdw,jcydjb.getFgzdw());
        queryWrapper.like(StringUtils.isNotBlank(jcydjb.getFgzdwdz()),SpcyRsJcydjb::getFgzdwdz,jcydjb.getFgzdwdz());
        queryWrapper.like(StringUtils.isNotBlank(jcydjb.getFzw()),SpcyRsJcydjb::getFzw,jcydjb.getFzw());
        queryWrapper.like(StringUtils.isNotBlank(jcydjb.getFzc()),SpcyRsJcydjb::getFzc,jcydjb.getFzc());
        queryWrapper.like(StringUtils.isNotBlank(jcydjb.getFdwyj()),SpcyRsJcydjb::getFdwyj,jcydjb.getFdwyj());
        queryWrapper.like(StringUtils.isNotBlank(jcydjb.getPhone()),SpcyRsJcydjb::getPhone,jcydjb.getPhone());
        queryWrapper.like(StringUtils.isNotBlank(jcydjb.getEmail()),SpcyRsJcydjb::getEmail,jcydjb.getEmail());
        queryWrapper.eq(StringUtils.isNotBlank(jcydjb.getFzhzt()),SpcyRsJcydjb::getFzhzt,jcydjb.getFzhzt());
        queryWrapper.eq(StringUtils.isNotBlank(jcydjb.getFshzt()),SpcyRsJcydjb::getFshzt,jcydjb.getFshzt());

        // 默认查询不等于废弃的数据
        if (StringUtils.isNotBlank(jcydjb.getFsjcjzt())){
            queryWrapper.eq(SpcyRsJcydjb::getFsjcjzt, jcydjb.getFsjcjzt());
        }else {
            queryWrapper.ne(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_FQ);
        }

        if (StringUtils.isNotBlank(jcydjb.getFjcylb())){
            queryWrapper.like(SpcyRsJcydjb::getFjcylb, jcydjb.getFjcylb());
        }

        queryWrapper.last("order by ftxrq desc nulls last,create_time desc");
        IPage<SpcyRsJcydjb> pageList = spcyRsJcydjbService.page(page, queryWrapper);
        return HttpResult.ok(pageList);

    }

    /**
     * 查询登记表数列
     * @param jcydjb
     * @param ids 登记表ids
     */
    public List<SpcyRsJcydjb> queryListDjb(SpcyRsJcydjb jcydjb, String ids, String startTime, String endTime){
        List<SpcyRsJcydjb> djbList = new ArrayList<>();

        if (StringUtils.isBlank(ids)) {

            LambdaQueryWrapper<SpcyRsJcydjb> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(StringUtils.isNotBlank(jcydjb.getFsfzh()), SpcyRsJcydjb::getFsfzh, jcydjb.getFsfzh());
//            queryWrapper.eq(StringUtils.isNotBlank(jcydjb.getRealname()), SpcyRsJcydjb::getRealname, jcydjb.getRealname());

            if (StringUtils.isNotBlank(jcydjb.getFsjcjzt())) {
                queryWrapper.eq(SpcyRsJcydjb::getFsjcjzt, jcydjb.getFsjcjzt());
            } else {
//                queryWrapper.ne(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_FQ);
                queryWrapper.eq(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_YTJ);
            }

            if (StringUtils.isNotBlank(jcydjb.getFjcylb())) {
//                List<String> lbList = StringPatternUtils.getListByIds(jcydjb.getFjcylb());
                queryWrapper.like(SpcyRsJcydjb::getFjcylb, jcydjb.getFjcylb());
            }

            if (StringUtils.isNotBlank(startTime) && StringUtils.isBlank(endTime)){
                queryWrapper.apply("to_char(ftxrq,'yyyy-mm-dd') >= {0}",startTime);
            }else if (StringUtils.isBlank(startTime) && StringUtils.isNotBlank(endTime)){
                queryWrapper.apply("to_char(ftxrq,'yyyy-mm-dd') <= {0}", endTime);
            }else if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){
                queryWrapper.apply("to_char(ftxrq,'yyyy-mm-dd') >= {0} and to_char(ftxrq,'yyyy-mm-dd') <= {1}",startTime, endTime);
            }

            queryWrapper.orderByDesc(SpcyRsJcydjb::getCreateTime);
            djbList = spcyRsJcydjbService.list(queryWrapper);

        }
        else {

            List<String> idsList = StringPatternUtils.getListByIds(ids);
            djbList = spcyRsJcydjbService.listByIds(idsList);

        }

        return djbList;
    }

    /**
     * 新增文章 - 根据登记表id
     */
    public SpcyRsJcydjfbwz addDjwz(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("登记表参数为空！请联系管理员！");
        }
        SpcyRsJcydjfbwz jcydjfbwz = new SpcyRsJcydjfbwz();
        jcydjfbwz.setId(GetUuIdUtils.ReplaceUuId());
        jcydjfbwz.setFjcydjbid(id);
        jcydjfbwz.setCreateTime(new Date());
        spcyRsJcydjfbwzService.save(jcydjfbwz);

        return jcydjfbwz;
    }

    /**
     * 新增工作经历 - 根据登记表id
     */
    public SpcyRsJcydjgzjl addDjgzjl(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("登记表参数为空！请联系管理员！");
        }

        SpcyRsJcydjgzjl jcydjgzjl = new SpcyRsJcydjgzjl();
        jcydjgzjl.setId(GetUuIdUtils.ReplaceUuId());
        jcydjgzjl.setFjcydjbid(id);
        jcydjgzjl.setFgzkssj(DateConversionUtils.convertStringToDateYearMonth(this.getGzjlKssj(id)));
        jcydjgzjl.setFgzjssjstr(JcydjbConstant.GZJL_END_TIME_ZJ);
        jcydjgzjl.setCreateTime(new Date());
        spcyRsJcydjgzjlService.save(jcydjgzjl);

        return jcydjgzjl;
    }

    private String getGzjlKssj(String id){
        String kssj = "";
        List<SpcyRsJcydjgzjl> list = spcyRsJcydjgzjlService.list(new LambdaQueryWrapper<SpcyRsJcydjgzjl>()
                .eq(SpcyRsJcydjgzjl::getFjcydjbid, id)
                .orderByDesc(SpcyRsJcydjgzjl::getCreateTime));
        if (CollectionUtil.isNotEmpty(list)){
            String fgzjssjstr = list.get(0).getFgzjssjstr();
            if (StringUtils.isNotBlank(fgzjssjstr)){
                if (fgzjssjstr.contains(JcydjbConstant.GZJL_END_TIME_ZJ)){
                    kssj = DateConversionUtils.DateToMonth(new Date());
                }else {
                    kssj = fgzjssjstr;
                }
            }else {
                kssj = DateConversionUtils.DateToMonth(new Date());
            }
        }else {
            kssj = DateConversionUtils.DateToMonth(new Date());
        }

        return kssj;
    }

    /**
     * 更新检查员登记表
     * @param buffer
     */
    public void updateJcydjb(SpcyRsJcydjb buffer) throws InvocationTargetException, IllegalAccessException {
        String id = buffer.getId();
        SpcyRsJcydjb Update = spcyRsJcydjbService.getById(id);
        if (Update == null){
            throw new BusinessException("当前登记表基本信息数据不存在，请重试！");
        }
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        try {
            Integer sex = Update.getSex();
            BeanUtils.populate(Update, BeanUtils.describe(buffer));
            if (buffer.getBirthday() != null){
                Update.setBirthday(buffer.getBirthday());
            }
            if (buffer.getSex() != null){
                sex = buffer.getSex();
            }
            Update.setSex(sex);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        spcyRsJcydjbService.updateById(Update);
    }

    /**
     * 更新发表文章
     * @param buffer
     */
    public void updateWz(SpcyRsJcydjfbwz buffer) throws InvocationTargetException, IllegalAccessException {
        String id = buffer.getId();

        SpcyRsJcydjfbwz Update = spcyRsJcydjfbwzService.getById(id);
        if (Update == null){
            throw new BusinessException("当前文章基本信息数据不存在，请重试！");
        }
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        try {
            BeanUtils.populate(Update, BeanUtils.describe(buffer));
            if (buffer.getFfbsj() != null){
                Update.setFfbsj(buffer.getFfbsj());
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        spcyRsJcydjfbwzService.updateById(Update);
    }


    /**
     * 更新工作经历
     * @param buffer
     */
    public void updateGzjl(SpcyRsJcydjgzjl buffer) throws InvocationTargetException, IllegalAccessException {
        String id = buffer.getId();

        SpcyRsJcydjgzjl Update = spcyRsJcydjgzjlService.getById(id);
        if (Update == null){
            throw new BusinessException("当前工作简历基本信息数据不存在，请重试！");
        }
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        try {
            BeanUtils.populate(Update, BeanUtils.describe(buffer));
            if (buffer.getFgzkssj() != null){
                Update.setFgzkssj(buffer.getFgzkssj());
            }
            if (buffer.getFgzjssj() != null){
                Update.setFgzjssj(buffer.getFgzjssj());
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        spcyRsJcydjgzjlService.updateById(Update);
    }

    /**
     * 更新类别
     * @param buffer
     */
    public void updateLb(HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException {
        String id = buffer.get("id");

        SpcyRsJcydjlb Update = spcyRsJcydjlbService.getById(id);
        if (Update == null){
            throw new BusinessException("当前基本信息数据不存在，请重试");
        }
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(Update, buffer);
        spcyRsJcydjlbService.updateById(Update);
    }

    /**
     * 更新账户状态
     * @param id 登记表id
     */
    public void updateZhzt(String id){
        if (StringUtils.isBlank(id)){
            return;
        }

        LambdaUpdateWrapper<SpcyRsJcydjb> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyRsJcydjb::getId, id);
        updateWrapper.eq(SpcyRsJcydjb::getFzhzt, JcydjbConstant.ZHZT_WSC);

        SpcyRsJcydjb update = new SpcyRsJcydjb();
        update.setFzhzt(JcydjbConstant.ZHZT_YSC);

        spcyRsJcydjbService.update(update, updateWrapper);
    }

    /**
     * 删除发表文章-根据文章id
     * @param id
     */
    public void deleteWz(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("删除失败，参数丢失！");
        }

        spcyRsJcydjfbwzService.removeById(id);
    }

    /**
     * 删除工作经历-根据工作经历id
     * @param id
     */
    public void deleteGzjl(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("删除失败，参数丢失！");
        }

        spcyRsJcydjgzjlService.removeById(id);
    }

    /**
     * 提交-检查员登记表
     * @param id
     */
    public void submitJcydjb(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("提交失败，登记表参数为空！");
        }
        SpcyRsJcydjb jcydjb = spcyRsJcydjbService.getById(id);
        if (jcydjb == null){
            throw new BusinessException("提交失败，登记表参数不存在！");
        }

        if (StringUtils.isBlank(jcydjb.getFsfzh())){
            throw new BusinessException("请填写身份证号！");
        }

        if (StringUtils.isBlank(jcydjb.getRealname())){
            throw new BusinessException("请填写姓名！");
        }

        /*if (jcydjb.getSex() == null){
            throw new BusinessException("请填写性别！");
        }

        if (jcydjb.getBirthday() == null){
            throw new BusinessException("请填写出生年月！");
        }

        if (StringUtils.isBlank(jcydjb.getFmz())){
            throw new BusinessException("请填写民族！");
        }

        if (StringUtils.isBlank(jcydjb.getFzzmm())){
            throw new BusinessException("请填写政治面貌！");
        }

        if (StringUtils.isBlank(jcydjb.getFsxzy())){
            throw new BusinessException("请填写所学专业！");
        }

        if (StringUtils.isBlank(jcydjb.getFxl())){
            throw new BusinessException("请填写学历！");
        }

        if (StringUtils.isBlank(jcydjb.getFzc())){
            throw new BusinessException("请填写职称！");
        }

        if (StringUtils.isBlank(jcydjb.getFzw())){
            throw new BusinessException("请填写职务！");
        }

        if (StringUtils.isBlank(jcydjb.getFgzdw())){
            throw new BusinessException("请填写工作单位及部门！");
        }

        if (StringUtils.isBlank(jcydjb.getFgzdwdz())){
            throw new BusinessException("请填写工作单位地址！");
        }

        if (StringUtils.isBlank(jcydjb.getFcsgz())){
            throw new BusinessException("请填写从事工作！");
        }

        if (StringUtils.isBlank(jcydjb.getFgznx())){
            throw new BusinessException("请填写工作年限！");
        }

        if (StringUtils.isBlank(jcydjb.getFqqhm())){
            throw new BusinessException("请填写QQ号码！");
        }

        */

        if (StringUtils.isBlank(jcydjb.getPhone())){
            throw new BusinessException("请填写手机号码！");
        }

        if (StringUtils.isBlank(jcydjb.getEmail())){
            throw new BusinessException("请填写邮箱！");
        }

       /* if (!CheckEmailUtils.checkEmail(jcydjb.getEmail())){
            throw new BusinessException("请填写正确的邮箱！");
        }*/

        if (StringUtils.isBlank(jcydjb.getFdwyj())){
            throw new BusinessException("请填写单位意见！");
        }

        /** 验证是否选择类别 */
        List<SpcyRsJcydjlb> lbList = this.queryJcyLbByDjbId(id, true);
        if (CollectionUtil.isEmpty(lbList)){
            throw new BusinessException("请至少选择一项检查员类别！");
        }

        /** 验证是否填写一条工作经历 */
        List<SpcyRsJcydjgzjl> gzjlList = this.queryJcydjGzjlByDjbId(id);
        if (CollectionUtil.isEmpty(gzjlList)){
            throw new BusinessException("请至少填写一条工作简历！");
        }else {
            for (int i=0; i<gzjlList.size(); i++){
                SpcyRsJcydjgzjl gzjl = gzjlList.get(i);
                if (gzjl.getFgzkssj() == null){
                    throw new BusinessException("请填写工作简历-开始时间！");
                }
                /*if (gzjl.getFgzjssj() == null){
                    throw new BusinessException("请填写工作简历-结束时间！");
                }*/
                if (StringUtils.isBlank(gzjl.getFgzjssjstr())){
                    throw new BusinessException("请填写工作简历-结束时间！");
                }
                if (StringUtils.isBlank(gzjl.getFgzdw())){
                    throw new BusinessException("请填写工作简历-单位名称！");
                }
                if (StringUtils.isBlank(gzjl.getFcsgz())){
                    throw new BusinessException("请填写工作简历-工作岗位！");
                }
                if (StringUtils.isBlank(gzjl.getFzw())){
                    throw new BusinessException("请填写工作简历-职务！");
                }
            }
        }

        // 复用检查员类别
        String lbstr = "";
        for (int i=0; i < lbList.size(); i++){
            if (StringUtils.isBlank(lbstr)) {
                lbstr += lbList.get(i).getFjcylb();
            }else {
                lbstr += "/"+lbList.get(i).getFjcylb();
            }
        }
        jcydjb.setFjcylb(lbstr);

        jcydjb.setFtxrq(new Date());
        jcydjb.setFsjcjzt(JcydjbConstant.SJCJZT_YTJ);
        spcyRsJcydjbService.updateById(jcydjb);

    }

    /**
     * 废弃-检查员登记表
     */
    public void fqJcydjb(String ids){
        List<String> idsList = StringPatternUtils.getListByIds(ids);

        for (int i=0; i<idsList.size(); i++) {
            SpcyRsJcydjb jcydjb = new SpcyRsJcydjb();
            jcydjb.setFsjcjzt(JcydjbConstant.SJCJZT_FQ);

            LambdaUpdateWrapper<SpcyRsJcydjb> wrapper = new LambdaUpdateWrapper();
            wrapper.eq(SpcyRsJcydjb::getId, idsList.get(i));

            spcyRsJcydjbService.update(jcydjb, wrapper);

            spcyLogService.addLogs("检查员数据采集","废弃",idsList.get(i),"废弃检查员采集数据", SpcyConstant.LOGS_SYS_TYPE_SC);
        }
    }

    /**
     * 验证手机号是否存在
     */
    public List<SpcyRsJcydjb> getJcydjbByRealname(String realname){
        List<SpcyRsJcydjb> userList = spcyRsJcydjbService.list(new LambdaQueryWrapper<SpcyRsJcydjb>()
                .eq(SpcyRsJcydjb::getRealname, realname)
                .ne(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_FQ));
        return userList;
    }

    /**
     * 验证手机号是否存在
     */
    public boolean verifyDjbPhone(String phone){

        List<SpcyRsJcydjb> userList = spcyRsJcydjbService.list(new LambdaQueryWrapper<SpcyRsJcydjb>()
                .eq(SpcyRsJcydjb::getPhone, phone)
                .ne(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_FQ));

        if (CollectionUtil.isNotEmpty(userList)){
            return true;
        }

        return false;
    }

    /** 验证手机号 */
    public Map verifyPhone(String phone){

        if (StringUtils.isBlank(phone)){
            throw new BusinessException("手机号不能为空！");
        }

        boolean flag = false;
        String djbid = "";
        SpcyRsJcydjb jcydjb = null;

        Map resultMap = new HashMap();

        // 不属于废弃的登记表数据
        List<SpcyRsJcydjb> djList = spcyRsJcydjbService.list(new LambdaQueryWrapper<SpcyRsJcydjb>()
                .eq(SpcyRsJcydjb::getPhone, phone)
                .ne(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_FQ));

        if (CollectionUtils.isNotEmpty(djList)){
            flag = true;
            djbid = djList.get(0).getId();
            jcydjb = djList.get(0);
        }

        resultMap.put("flag", flag);
        resultMap.put("id", djbid);
        resultMap.put("jcydjb", jcydjb);

        return resultMap;
    }

    /**
     * 验证身份证是否存在
     * @return
     * map {
     *     flag: true: 存在；false: 不存在(默认)
     *     id : 检查员登记表id
     *     jcydjb: 检查员登记表
     * }
     */
    public Map verifyIdCard(String fsfzh){

        if (StringUtils.isBlank(fsfzh)){
            throw new BusinessException("身份证号不能为空！");
        }

        boolean flag = false;
        String djbid = "";
        SpcyRsJcydjb jcydjb = null;

        Map resultMap = new HashMap();

        // 不属于废弃的登记表数据
        List<SpcyRsJcydjb> djList = spcyRsJcydjbService.list(new LambdaQueryWrapper<SpcyRsJcydjb>()
                .eq(SpcyRsJcydjb::getFsfzh, fsfzh)
                .ne(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_FQ));

        if (CollectionUtils.isNotEmpty(djList)){
            flag = true;
            djbid = djList.get(0).getId();
            jcydjb = djList.get(0);
        }

        resultMap.put("flag", flag);
        resultMap.put("id", djbid);
        resultMap.put("jcydjb", jcydjb);

        return resultMap;
    }

    /**
     * 初始化 重载 检查员登记表 - 并返回登记信息详情
     * @param fsfzh 身份证号
     * @param id 登记表id
     * @return
     *  map {
     *     jbxx: 登记基本信息
     *     lbList: 类别
     *     gzjlList: 工作经历
     *     wzList: 发表文章
     *  }
     */
    public Map initReloadJcydjb(String fsfzh, String id, String code){

        SpcyRsJcydjb jcydjb = null;
        List<SpcyRsJcydjlb> lbList = new ArrayList<>();
        String jcydjbid = "";

        if (StringUtils.isNotBlank(id)){
            jcydjbid = id;
        }

        if (StringUtils.isBlank(id)) {
            // 验证身份证是否存在
            Map map = this.verifyIdCard(fsfzh);
            boolean existSfzh = (Boolean) map.get("flag");

            // 存在
            if (existSfzh) {
                jcydjbid = map.get("id").toString();
                jcydjb = (SpcyRsJcydjb) map.get("jcydjb");

                if (jcydjb != null){
                    /*if (StringUtils.isBlank(jcydjb.getFyzm())) {
                        throw new BusinessException("请发送验证码！");
                    }

                    if (!jcydjb.getFyzm().equals(code)){
                        throw new BusinessException("验证码错误，请重新输入！");
                    }*/

                    /*if (jcydjb.getFyzmyxq() != null){
                        Date nowTime = new Date();
                        long nowSj = nowTime.getTime(); // 当前时间
                        long oldSj = jcydjb.getFyzmyxq().getTime(); // 有效期

                        if (nowSj > oldSj){
                            throw new BusinessException("验证码有效期失效，请重新发送验证码！");
                        }
                    }*/
                }

            }else {
                throw new BusinessException("请先发送验证码！");
            } /*else {

                // 不存在
                jcydjb = this.autoInitJcydjbBySfzh(fsfzh);

                // 自动生成-登记-类别
                lbList = this.autoInitJcydjLb(jcydjb.getId());

            }*/

        }

        // 查询详情
        Map resultMap = queryJcydjbInfo(jcydjb, lbList, jcydjbid);

        return resultMap;
    }

    /**
     * 根据身份证号-自动生成-登记表基本信息
     * @param fsfzh
     * @return
     */
    public SpcyRsJcydjb autoInitJcydjbBySfzh(String fsfzh){
        String id = GetUuIdUtils.ReplaceUuId();
        SpcyRsJcydjb jcydjb = new SpcyRsJcydjb();
        jcydjb.setId(id);
        jcydjb.setFsfzh(fsfzh);
        jcydjb.setFsjcjzt(JcydjbConstant.SJCJZT_TXZ);
        jcydjb.setCreateTime(new Date());
        spcyRsJcydjbService.save(jcydjb);

        return jcydjb;
    }

    /**
     * 根据身份证号-手机号-验证码-自动生成-登记表基本信息
     * @param fsfzh
     * @return
     */
    public SpcyRsJcydjb autoInitJcydjbBySfzh(String fsfzh, String phone, String code, Date yxq, boolean existSfzh,SpcyRsJcydjb jcydjb){


        Map map = this.verifyIdCard(fsfzh);
        boolean existSfzh2 = (Boolean) map.get("flag");
        // 存在
        if (existSfzh2) {
            jcydjb = (SpcyRsJcydjb) map.get("jcydjb");

            jcydjb.setPhone(phone);
            jcydjb.setFyzm(code);
            jcydjb.setFyzmyxq(yxq);
            spcyRsJcydjbService.updateById(jcydjb);

        }else {

            String id = GetUuIdUtils.ReplaceUuId();
            jcydjb = new SpcyRsJcydjb();
            jcydjb.setId(id);
            jcydjb.setFsfzh(fsfzh);
            jcydjb.setPhone(phone);
            jcydjb.setFzhzt(JcydjbConstant.ZHZT_WSC);

            // 解析身份证号获取出生年月
            try {
                Date birthDayFromIdCard = DateConversionUtils.getBirthDayFromIdCard(fsfzh);
                if (birthDayFromIdCard != null){
                    jcydjb.setBirthday(birthDayFromIdCard);
                }
            } catch (ParseException e) {
                log.error("身份证解析失败！");
            }

            // 获取性别
            int sex = DateConversionUtils.getSexFromIdCard(fsfzh);
            jcydjb.setSex(sex);

            jcydjb.setFsjcjzt(JcydjbConstant.SJCJZT_TXZ);
            jcydjb.setCreateTime(new Date());
            jcydjb.setFyzm(code);
            jcydjb.setFyzmyxq(yxq);
            spcyRsJcydjbService.save(jcydjb);

            // 自动生成-登记-类别
            this.autoInitJcydjLb(id);
        }

        return jcydjb;
    }

    /**
     * 根据登记表id - 自动生成-登记-类别
     */
    public List<SpcyRsJcydjlb> autoInitJcydjLb(String djbid){
        String lbs = this.getSysConfigByCode(JcydjbConstant.SYS_CONFIG_LBS_CODE);
        List<String> lbList = StringPatternUtils.getListByIds(lbs);

        List<SpcyRsJcydjlb> lbsList = new ArrayList<>();
        int order = 1;
        for (String lb : lbList){
            SpcyRsJcydjlb jcydjlb = new SpcyRsJcydjlb();
            jcydjlb.setId(GetUuIdUtils.ReplaceUuId());
            jcydjlb.setFjcydjbid(djbid);
            jcydjlb.setFcjcs("0");

//            String jctc = this.getJctcWithLb(lb);
            jcydjlb.setFjctz("");

            jcydjlb.setFjcylb(lb);
            jcydjlb.setFxzzt("0");
            jcydjlb.setCreateTime(new Date());
            jcydjlb.setForder(order);
            lbsList.add(jcydjlb);

            order++;
        }

        if (CollectionUtils.isNotEmpty(lbsList)){
            spcyRsJcydjlbService.saveBatch(lbsList);
        }

        return lbsList;
    }

    /**
     * 根据检查员登记表id
     * @param id
     * @return
     * map {
     *     djb: 登记基本信息
     *     lbList: 类别
     *     gzjlList: 工作经历
     *     wzList: 发表文章
     * }
     */
    public Map queryJcydjbInfo(SpcyRsJcydjb jcydjb, List<SpcyRsJcydjlb> lbList, String id){
        Map resultMap = new HashMap();

        // 基本信息
        if (jcydjb == null) {
            jcydjb = this.queryJcydjbById(id);
        }

        // 类别
        resultMap.putAll(this.queryJcydjbLbByDjbId(id,lbList,false));

        // 工作经历
        List<SpcyRsJcydjgzjl> gzjlList = this.queryJcydjGzjlByDjbId(id);

        // 发表文章
        List<SpcyRsJcydjfbwz> wzList = this.queryJcydjFbwzByDjbId(id);


        resultMap.put("djb", jcydjb);
//        resultMap.put("lbList", lbList);
        resultMap.put("gzjlList", gzjlList);
        resultMap.put("wzList", wzList);

        return resultMap;
    }

    /**
     * 根据id-查询检查员登记表-基本信息
     * @param id
     * @return
     */
    public SpcyRsJcydjb queryJcydjbById(String id){
        SpcyRsJcydjb jcydjb = spcyRsJcydjbService.getById(id);

        if (jcydjb == null){
            throw new BusinessException("当前登记信息丢失！请联系管理员！");
        }

        return jcydjb;
    }

    /**
     * 根据检查员登记表id-查询检查员登记表-类别
     * @param id
     * @return
     */
    public Map queryJcydjbLbByDjbId(String id,List<SpcyRsJcydjlb> lbList,boolean isSfxz){

        Map resultMap = new HashMap();
        List<Map> resultList = new ArrayList<>();

        if (CollectionUtils.isEmpty(lbList)) {

            LambdaQueryWrapper<SpcyRsJcydjlb> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SpcyRsJcydjlb::getFjcydjbid, id);
            wrapper.orderByAsc(SpcyRsJcydjlb::getForder);

            if (isSfxz) {
                wrapper.eq(SpcyRsJcydjlb::getFxzzt, "1");
            }

            lbList = spcyRsJcydjlbService.list(new LambdaQueryWrapper<SpcyRsJcydjlb>()
                    .eq(SpcyRsJcydjlb::getFjcydjbid, id)
                    .orderByAsc(SpcyRsJcydjlb::getForder));
        }

        if (CollectionUtils.isNotEmpty(lbList)){
            for (SpcyRsJcydjlb jcydjlb : lbList){
                Map resultMap1 = new HashMap();
                resultMap1.put("lb", jcydjlb);
                resultMap1.put("typeList", this.getJctcWithLb(jcydjlb.getFjcylb()));
                resultList.add(resultMap1);
            }
        }


        resultMap.put("lbList",resultList);

        return resultMap;
    }

    /**
     * 根据检查员登记表id-查询检查员类别
     * @param id 登记表id
     * @param isSfxz true：选中
     * @return
     */
    public List<SpcyRsJcydjlb> queryJcyLbByDjbId(String id,boolean isSfxz){
        LambdaQueryWrapper<SpcyRsJcydjlb> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SpcyRsJcydjlb::getFjcydjbid, id);
        wrapper.orderByAsc(SpcyRsJcydjlb::getForder);

        if (isSfxz){
            wrapper.eq(SpcyRsJcydjlb::getFxzzt, "1");
        }

        List<SpcyRsJcydjlb> lbList = spcyRsJcydjlbService.list(wrapper);

        return lbList;
    }

    /**
     * 根据检查员登记表id-查询检查员登记表-工作经历
     * @param id
     * @return
     */
    public List<SpcyRsJcydjgzjl> queryJcydjGzjlByDjbId(String id){

        List<SpcyRsJcydjgzjl> gzjlList = spcyRsJcydjgzjlService.list(new LambdaQueryWrapper<SpcyRsJcydjgzjl>()
                .eq(SpcyRsJcydjgzjl::getFjcydjbid, id)
                .orderByAsc(SpcyRsJcydjgzjl::getCreateTime));

        return gzjlList;
    }

    /**
     * 根据检查员登记表id-查询检查员登记表-发表文章
     * @param id
     * @return
     */
    public List<SpcyRsJcydjfbwz> queryJcydjFbwzByDjbId(String id){

        List<SpcyRsJcydjfbwz> List = spcyRsJcydjfbwzService.list(new LambdaQueryWrapper<SpcyRsJcydjfbwz>()
                .eq(SpcyRsJcydjfbwz::getFjcydjbid, id)
                .orderByAsc(SpcyRsJcydjfbwz::getCreateTime));

        return List;
    }

    /**
     * 下载检查员登记表-根据登记表id
     * @param id
     */
    public void downloadDjbFile(String id, boolean isReloadWj, HttpServletResponse response){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("参数为空！");
        }
        SpcyRsJcydjb jcydjb = spcyRsJcydjbService.getById(id);
        if (jcydjb == null){
            throw new BusinessException("查无对应数据！");
        }

        if (StringUtils.isNotBlank(jcydjb.getFsjcjzt()) && !jcydjb.getFsjcjzt().equals(JcydjbConstant.SJCJZT_YTJ)
                && !jcydjb.getFsjcjzt().equals(JcydjbConstant.SJCJZT_YLZ)){
            throw new BusinessException("下载失败！当前数据还没有提交无法下载相关文件！");
        }

        boolean isExistWjjl = false;
        if (StringUtils.isNotBlank(jcydjb.getFcfwjlj())){
            isExistWjjl = true;
        }

        if (!isExistWjjl || isReloadWj) {
            Map<String, String> templateField = this.getTemplateField(jcydjb, id);

            String orgUrl = businessConstant.UPLOAD_FILE_ROOT + "/" + JcydjbConstant.JCYDJB_TEMPLATE_NAME;
            String destWordUrl = ""; // word保存路径
            String destPdfUrl = ""; // pdf保存路径
            String wjlx = Util.getWjHz(orgUrl); // 获取文件路径的 文件类型
            String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

            destWordUrl = localFileUploadUtils.getTempFileRootUrl() + "/";
            destPdfUrl = destWordUrl;
            destWordUrl += GetUuIdUtils.ReplaceUuId() + wjlx;
            destPdfUrl += GetUuIdUtils.ReplaceUuId() + ".pdf";

            boolean flag = InsertAndOutWordUtils.wordTextSubstitutionTwo(orgUrl, destWordUrl, formatType, templateField);// word关键字替换文本,并生成新文件

            if (flag) {
                wordPdfUtils.wordToPdf(destWordUrl, destPdfUrl);

                String cfwjlj = "/jcydjb/" + GetUuIdUtils.ReplaceUuId() + ".pdf";
                if (localFileUploadUtils.isEnableMinioWjjlFile()) {
                    spcyMinioUtils.putFile(cfwjlj, new File(destPdfUrl), ".pdf");
                }else {
                    FileCopyUtils.copyFile(destPdfUrl, localFileUploadUtils.getZdyFileAbsUrl(cfwjlj));
                }
                jcydjb.setFcfwjlj(cfwjlj);
                spcyRsJcydjbService.updateById(jcydjb);

                localFileUploadUtils.ejzLocalFileByZdyFile(new File(destPdfUrl), response);
            }
        }

        if (isExistWjjl || !isReloadWj) {
            if (localFileUploadUtils.isEnableMinioWjjlFile()) {
                spcyMinioUtils.download(jcydjb.getFcfwjlj(), response);
            }else {
                localFileUploadUtils.ejzLocalFileByZdyFile(new File(jcydjb.getFcfwjlj()), response);
            }
        }
    }

    /**
     * 预览检查员登记表
     */
    public void previewFile(String id, HttpServletResponse response){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("无效参数！");
        }
        SpcyRsJcydjb jcydjb = spcyRsJcydjbService.getById(id);
        if (jcydjb == null){
            throw new BusinessException("查无对应数据！");
        }

        boolean isExistWjjl = false;

        if (!isExistWjjl) {
            Map<String, String> templateField = this.getTemplateField(jcydjb, id);

            String orgUrl = businessConstant.UPLOAD_FILE_ROOT + "/" + JcydjbConstant.JCYDJB_TEMPLATE_NAME;
            String destWordUrl = ""; // word保存路径
            String destPdfUrl = ""; // pdf保存路径
            String wjlx = Util.getWjHz(orgUrl); // 获取文件路径的 文件类型
            String formatType = BusinessConstant.getWordFormatType(wjlx); // word替换格式

            destWordUrl = localFileUploadUtils.getTempFileRootUrl() + "/";
            destPdfUrl = destWordUrl;
            destWordUrl += GetUuIdUtils.ReplaceUuId() + wjlx;
            destPdfUrl += GetUuIdUtils.ReplaceUuId() + ".pdf";

            boolean flag = InsertAndOutWordUtils.wordTextSubstitutionTwo(orgUrl, destWordUrl, formatType, templateField);// word关键字替换文本,并生成新文件

            if (flag) {
                wordPdfUtils.wordToPdf(destWordUrl, destPdfUrl);
                localFileUploadUtils.ejzLocalFileByZdyFile(new File(destPdfUrl), response);
            }
        }

    }

    /**
     * 预览-检查员登记表
     */
    public void submitPreview(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("提交失败，登记表参数为空！");
        }
        SpcyRsJcydjb jcydjb = spcyRsJcydjbService.getById(id);
        if (jcydjb == null){
            throw new BusinessException("提交失败，登记表参数不存在！");
        }

        if (StringUtils.isBlank(jcydjb.getFsfzh())){
            throw new BusinessException("请填写身份证号！");
        }

        if (StringUtils.isBlank(jcydjb.getRealname())){
            throw new BusinessException("请填写姓名！");
        }

        if (StringUtils.isBlank(jcydjb.getPhone())){
            throw new BusinessException("请填写手机号码！");
        }

        if (StringUtils.isBlank(jcydjb.getEmail())){
            throw new BusinessException("请填写邮箱！");
        }

        if (StringUtils.isBlank(jcydjb.getFdwyj())){
            throw new BusinessException("请填写单位意见！");
        }

        /** 验证是否选择类别 */
        List<SpcyRsJcydjlb> lbList = this.queryJcyLbByDjbId(id, true);
        if (CollectionUtil.isEmpty(lbList)){
            throw new BusinessException("请至少选择一项检查员类别！");
        }

        /** 验证是否填写一条工作经历 */
        List<SpcyRsJcydjgzjl> gzjlList = this.queryJcydjGzjlByDjbId(id);
        if (CollectionUtil.isEmpty(gzjlList)){
            throw new BusinessException("请至少填写一条工作简历！");
        }else {
            for (int i=0; i<gzjlList.size(); i++){
                SpcyRsJcydjgzjl gzjl = gzjlList.get(i);
                if (gzjl.getFgzkssj() == null){
                    throw new BusinessException("请填写工作简历-开始时间！");
                }
                if (StringUtils.isBlank(gzjl.getFgzjssjstr())){
                    throw new BusinessException("请填写工作简历-结束时间！");
                }
                if (StringUtils.isBlank(gzjl.getFgzdw())){
                    throw new BusinessException("请填写工作简历-单位名称！");
                }
                if (StringUtils.isBlank(gzjl.getFcsgz())){
                    throw new BusinessException("请填写工作简历-工作岗位！");
                }
                if (StringUtils.isBlank(gzjl.getFzw())){
                    throw new BusinessException("请填写工作简历-职务！");
                }
            }
        }

        jcydjb.setFsjcjzt(JcydjbConstant.SJCJZT_YLZ);
        spcyRsJcydjbService.updateById(jcydjb);
    }

    /**
     * 预览退回填写
     */
    public void previewReturn(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("无效参数！");
        }

        LambdaUpdateWrapper<SpcyRsJcydjb> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcyRsJcydjb::getId, id);
        updateWrapper.eq(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_YLZ);

        SpcyRsJcydjb jcydjb = new SpcyRsJcydjb();
        jcydjb.setFsjcjzt(JcydjbConstant.SJCJZT_TXZ);
        jcydjb.setFcfwjlj("");
        spcyRsJcydjbService.update(jcydjb, updateWrapper);
    }

    /**
     * 获取检查员登记表-字段模板
     * @param id
     * @return
     */
    public Map<String,String> getTemplateField(SpcyRsJcydjb jcydjb, String id){

        Map<String,String> objectMap = new HashMap<>();

        if (jcydjb == null) {
            jcydjb = spcyRsJcydjbService.getById(id);
        }
        if (jcydjb == null){
            throw new BusinessException("当前数据不存在！请刷新重试！");
        }

        String defaultParam = "无";
        String csny = defaultParam; // 出生年月
        String xb = defaultParam; // 性别
        Map<String, String> map = this.objectToMap(jcydjb);
        objectMap.putAll(map);

        // 出生年月
        if (jcydjb.getBirthday() != null){
            csny = DateConversionUtils.DateToMonth(jcydjb.getBirthday());
        }

        // 性别
        if (jcydjb.getSex() != null){
            xb = String.valueOf(jcydjb.getSex());
            if (xb.equals("1")){
                xb = "男";
            }else {
                xb = "女";
            }
        }

        objectMap.put("${data.csny}",csny);
        objectMap.put("${data.xb}",xb);


        String nowTime = "无"; // 当前时间
        String nowYear = "无"; // 当前年份

        nowTime = DateConversionUtils.DateToYMDString(DateConversionUtils.DateToString(new Date())); // 当前时间
        objectMap.put("${data.nowTime}", nowTime);

        // 当前年份
        if (jcydjb.getFtxrq() == null){
            nowYear = DateConversionUtils.DateToYear(new Date());
        }else {
            nowYear = DateConversionUtils.DateToYear(jcydjb.getFtxrq());
        }
        objectMap.put("nowYear", nowYear);

        StringBuffer lb = new StringBuffer();
        List<SpcyRsJcydjlb> lbList = this.queryJcyLbByDjbId(id,true);
        if (CollectionUtils.isNotEmpty(lbList)){
            int count = 1;
            for (SpcyRsJcydjlb jcydjlb : lbList){
                if (count>1){
                    lb.append("\n");
                }
                lb.append(jcydjlb.getFjcylb());
                lb.append("，共检查次数（").append(Optional.ofNullable(jcydjlb.getFcjcs()).orElseGet(()->defaultParam)).append("）");
                lb.append("；特长：").append(Optional.ofNullable(jcydjlb.getFjctz()).orElseGet(()->defaultParam));
                lb.append("；");
                count++;
            }
        }

        StringBuffer gzjl = new StringBuffer();
        List<SpcyRsJcydjgzjl> spcyRsJcydjgzjls = this.queryJcydjGzjlByDjbId(id);
        if (CollectionUtils.isNotEmpty(spcyRsJcydjgzjls)){
            int count = 1;
            for (SpcyRsJcydjgzjl jcydjgzjl : spcyRsJcydjgzjls){

                if (count > 1){
                    gzjl.append("\n");
                }

                boolean kssj = true;
                if (jcydjgzjl.getFgzkssj() != null){
                    gzjl.append(DateConversionUtils.DateToMonth(jcydjgzjl.getFgzkssj()));
                }else {
                    kssj = false;
                    gzjl.append(defaultParam);
                }

                /*if (jcydjgzjl.getFgzjssj() != null){
                    gzjl.append("至");
                    gzjl.append(DateConversionUtils.DateToMonth(jcydjgzjl.getFgzjssj()));
                }else {
                    if (kssj){
                        gzjl.append("至");
                        gzjl.append(defaultParam);
                    }
                }*/
                if (StringUtils.isNotBlank(jcydjgzjl.getFgzjssjstr())){
                    if (jcydjgzjl.getFgzjssjstr().contains(JcydjbConstant.GZJL_END_TIME_ZJ)){
                        gzjl.append(JcydjbConstant.GZJL_END_TIME_ZJ);
                    }else {
                        gzjl.append("至");
                        gzjl.append(jcydjgzjl.getFgzjssjstr());
                    }
                }else {
                    if (kssj){
                        gzjl.append("至");
                        gzjl.append(defaultParam);
                    }
                }

                gzjl.append("\t");
                gzjl.append(Optional.ofNullable(jcydjgzjl.getFgzdw()).orElseGet(()->defaultParam));
                gzjl.append("\t");
                gzjl.append(Optional.ofNullable(jcydjgzjl.getFcsgz()).orElseGet(()->defaultParam));
                gzjl.append("\t");
                gzjl.append(Optional.ofNullable(jcydjgzjl.getFzw()).orElseGet(()->defaultParam));
                gzjl.append("；");

                count++;
            }
        }else {
            gzjl.append(defaultParam);
        }

        StringBuffer wz = new StringBuffer();
        List<SpcyRsJcydjfbwz> wzList = this.queryJcydjFbwzByDjbId(id);
        if (CollectionUtils.isNotEmpty(wzList)){
            int count = 1;
            for (SpcyRsJcydjfbwz jcydjfbwz : wzList){

                if (count > 1){
                    wz.append("\n");
                }

                if (jcydjfbwz.getFfbsj() != null){
                    wz.append(DateConversionUtils.DateToMonth(jcydjfbwz.getFfbsj()));
                }else {
                    wz.append(defaultParam);
                }

                wz.append("\t");
                wz.append(Optional.ofNullable(jcydjfbwz.getFqkmc()).orElseGet(()->defaultParam));
                wz.append("\t");
                wz.append(Optional.ofNullable(jcydjfbwz.getFwzmc()).orElseGet(()->defaultParam));
                wz.append("；");

                count++;
            }
        }else {
            wz.append(defaultParam);
        }

        objectMap.put("${data.lb}", lb.toString());
        objectMap.put("${data.gzjl}", gzjl.toString());
        objectMap.put("${data.wz}", wz.toString());
        return objectMap;
    }

    /**
     * 将对象转成Map对象-模板字段
     * @param object
     * @return
     */
    public static Map<String,String> objectToMap(Object object) {
        Map<String,String> dataMap = new HashMap<>();
        BeanMap beanMap = BeanMap.create(object);
        for (Object key : beanMap.keySet()) {
            String k = "${data."+key+"}";
            if (beanMap.get(key) != null){
                dataMap.put(k,beanMap.get(key).toString());
            }else {
                dataMap.put(k,"无");
            }
        }
        return dataMap;
    }

    /**
     * 根据身份证号-获取手机号
     */
    public String getPhoneBySfzh(String fsfzh){
        String phone = "";

        if (StringUtils.isBlank(fsfzh)){
            throw new BusinessException("请填写身份证号！");
        }

        List<SpcyRsJcydjb> list = spcyRsJcydjbService.list(new LambdaQueryWrapper<SpcyRsJcydjb>()
                .eq(SpcyRsJcydjb::getFsfzh, fsfzh));

        if (CollectionUtils.isNotEmpty(list)){
            phone = list.get(0).getPhone();
        }

        return phone;
    }

    /**
     * 发送验证码
     */
    public void sendYzm(String fsfzh, String phone) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
        if (StringUtils.isBlank(phone)){
            throw new BusinessException("请输入正确的手机号！");
        }

        // 验证身份证号
        SpcyRsJcydjb jcydjb = null;
        Map map = this.verifyIdCard(fsfzh);
        boolean existSfzh = (Boolean) map.get("flag");
        if (existSfzh) {
            jcydjb = (SpcyRsJcydjb) map.get("jcydjb");

            if (StringUtils.isNotBlank(jcydjb.getPhone()) && !jcydjb.getPhone().equals(phone)) {
                // 验证手机号是否存在
                boolean isExistPhone = this.verifyDjbPhone(phone);
                if (isExistPhone) {
                    throw new BusinessException("手机号：[" + phone + "]已经被占用！");
                }
            }
        }else {
            // 验证手机号是否存在
            boolean isExistPhone = this.verifyDjbPhone(phone);
            if (isExistPhone){
                throw new BusinessException("手机号：["+phone+"]已经被占用！");
            }
        }

//        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        String code = "3781";
        String msg = "验证码：";
        msg += code;
        msg += "，用于全区两品一械检查员重新登记表填写，30分钟内有效。";

        String flag = SysConfigConstant.SMS_FLAG;
        // 发送短信成功
        if (flag.contains(SysConfigConstant.SMS_FLAG)){
            Date nowTime = new Date();
            Date yxq = DateUtil.offsetMinute(nowTime, 30); // 验证码有效期


            this.autoInitJcydjbBySfzh(fsfzh, phone, code, yxq, existSfzh, jcydjb);
        }else {
            throw new BusinessException("发送手机短信验证码失败！请确认手机号是否正确！");
        }

    }

    /**
     * 登记表id-发送短信下载登记表
     * @param id
     */
    public void sendFileSms(String phone, String id){
        String msg = "";
        msg += "恭喜您已完成全区两品一械检查员重新登记，";
        msg += "请您下载登记表: " + ClientJumpUrlConstant.Https_pro + ClientJumpUrlConstant.Down_Jcydjb_Url+id;
        msg += "，加盖单位公章后发送到xxxx。";
        msg += "若直接点击链接无法下载，请复制链接到电脑浏览器进行下载。";

    }

    /**
     * 给填写中的检查员登记表发送短信
     */
    public void sendSmsToTxzJcydjb() throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
        String msg = "您好，您通过扫码填报的两品一械检查员重新登记信息，系统状态显示为“填写中”，请您1.确认本人为自治区药监局在库的正式检查员；2.于2023年11月21日24时前再次扫二维码完善信息并提交；3.打印生成的重新登记表加盖单位公章，扫描发送至670594222@qq.com。逾期未完成提交和发送至邮箱的视为放弃重新登记资格。";
        List<SpcyRsJcydjb> list = spcyRsJcydjbService.list(new LambdaQueryWrapper<SpcyRsJcydjb>()
                .eq(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_TXZ));

        if (CollectionUtil.isNotEmpty(list)){
            for (SpcyRsJcydjb jcydjb : list){
                if (StringUtils.isNotBlank(jcydjb.getPhone())) {
                    String flag = smsUtils.sendSmsZdy(jcydjb.getPhone(),msg);
                    // 发送短信成功
                    if (flag.contains(SysConfigConstant.SMS_FLAG)) {
                        spcySmsLogService.autoAddSmsLog(jcydjb.getPhone(), msg);
                    }
                }
            }
        }

    }

    /**
     * 导出登记表excel表格详情-根据登记ids或条件
     */
    public void exportExcelJcydjxq(SpcyRsJcydjb jcydjb, ParamsBo paramsBo, String startTime, String endTime, HttpServletResponse response){
        // 查询登记表数据
        List<SpcyRsJcydjb> djbList = this.queryListDjb(jcydjb, paramsBo.getIds(), startTime, endTime);

        List<List<String>> resultList = new ArrayList<>();

        // 表头
//        String[] titles = new String[]{"序号","姓名","性别","身份证号码","手机号","QQ号码","邮箱","工作单位","职称","检查员类别","单位意见"};
        String[] titles = new String[]{"序号","姓名","身份证号","性别","工作单位","检查员类别","手机号","组长/组员/预备检查员","备注"};
        List<String> titleList = CollectionUtil.newArrayList(titles);
        resultList.add(titleList);

        if (CollectionUtil.isNotEmpty(djbList)){

            for (int i = 0; i < djbList.size(); i++){
                SpcyRsJcydjb bean = djbList.get(i);

                // 数据
                List<String> dataList = new ArrayList<>();

                dataList.add(String.valueOf(i+1));
                dataList.add(bean.getRealname());
                dataList.add(bean.getFsfzh());

                Integer sex = bean.getSex();
                if (sex != null) {
                    if (sex == 1) {
                        dataList.add("男");
                    }else if (sex == 2) {
                        dataList.add("女");
                    }else {
                        dataList.add("未知");
                    }
                }else {
                    dataList.add("未知");
                }

                dataList.add(bean.getFgzdw());

//                dataList.add(bean.getFsfzh());
//                dataList.add(bean.getPhone());
//                dataList.add(bean.getFqqhm());
//                dataList.add(bean.getEmail());
//                dataList.add(bean.getFgzdw());
//                dataList.add(bean.getFzc());

                String lb = "";
                // 查询对应选中的类别
                List<SpcyRsJcydjlb> lbList = this.queryJcyLbByDjbId(bean.getId(), true);
                for (int j = 0; j < lbList.size(); j++){
                    if (StringUtils.isBlank(lb)) {
                        lb += lbList.get(j).getFjcylb();
                    }else {
                        lb += ","+lbList.get(j).getFjcylb();
                    }
                }

                dataList.add(lb);

                dataList.add(bean.getPhone());

                dataList.add("");
                dataList.add("");

//                dataList.add(bean.getFdwyj());
                /*dataList.add("");
                dataList.add("");*/

                resultList.add(dataList);
            }

        }

        // 文件名称
        String title = "全区两品一械检查员重新登记表"+"("+DateConversionUtils.DateToYear(new Date())+")";

        String rootUrl = localFileUploadUtils.getTempFileRootUrl();

        File outFile = ExcelUtils.getLocalDempXlsExcel(resultList, title, rootUrl);

        localFileUploadUtils.ejzLocalFileByZdyFile(outFile,response);

    }

    /**
     * 将数据采集状态已提交信息修改为填写中
     * @param paramsBo ids选择多条数据
     */
    public void updateSjcjztYtjToTxz(ParamsBo paramsBo){
        if (StringUtils.isBlank(paramsBo.getIds())){
            throw new BusinessException("请选择已提交的数据！");
        }

        List<String> idsList = StringPatternUtils.getListByIds(paramsBo.getIds());

        for (int i=0; i<idsList.size(); i++){
            LambdaUpdateWrapper<SpcyRsJcydjb> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SpcyRsJcydjb::getId, idsList.get(i));
            updateWrapper.eq(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_YTJ);

            SpcyRsJcydjb jcydjb = new SpcyRsJcydjb();
            jcydjb.setFsjcjzt(JcydjbConstant.SJCJZT_TXZ);
            jcydjb.setFcfwjlj("");
            spcyRsJcydjbService.update(jcydjb, updateWrapper);
        }
    }

    /**
     * 将所有检查员类别按类别分别导出excel，放入zip
     */
    public void exportAllJcylbExcelZip(String fsjcjzt,String startTime,String endTime,HttpServletResponse response){
        // 获取所有类别
        String lbs = this.getSysConfigByCode(JcydjbConstant.SYS_CONFIG_LBS_CODE);
        List<String> lbList = StringPatternUtils.getListByIds(lbs);

//        String[] titles = new String[]{"序号","身份证号","姓名","性别","工作单位","手机号","检查员类别","组长/组员/预备检查员"};
        String[] titles = new String[]{"序号","姓名","身份证号","性别","工作单位","检查员类别","手机号","组长/组员/预备检查员","备注"};
        List<String> titleList = CollectionUtil.newArrayList(titles);

        List<File> files = new ArrayList<>();
        List<String> fileNames = new ArrayList<>();

        for (String lb : lbList){

            List<List<String>> resultList = new ArrayList<>();
            resultList.add(titleList);

            // 对应类别-已提交的信息
            LambdaQueryWrapper<SpcyRsJcydjb> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(SpcyRsJcydjb::getFjcylb, lb);
//            queryWrapper.eq(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_YTJ);
            queryWrapper.eq(StringUtils.isNotBlank(fsjcjzt), SpcyRsJcydjb::getFsjcjzt, fsjcjzt);

            if (StringUtils.isNotBlank(startTime) && StringUtils.isBlank(endTime)){
                queryWrapper.apply("to_char(ftxrq,'yyyy-mm-dd') >= {0}",startTime);
            }else if (StringUtils.isBlank(startTime) && StringUtils.isNotBlank(endTime)){
                queryWrapper.apply("to_char(ftxrq,'yyyy-mm-dd') <= {0}", endTime);
            }else if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)){
                queryWrapper.apply("to_char(ftxrq,'yyyy-mm-dd') >= {0} and to_char(ftxrq,'yyyy-mm-dd') <= {1}",startTime, endTime);
            }

            queryWrapper.orderByAsc(SpcyRsJcydjb::getFtxrq);
            List<SpcyRsJcydjb> jcydjbList = spcyRsJcydjbService.list(queryWrapper);

            for (int i=0; i<jcydjbList.size(); i++){
                List<String> dataList = new ArrayList<>();

                dataList.add(String.valueOf(i+1));
//                dataList.add(jcydjbList.get(i).getFsfzh());
                dataList.add(jcydjbList.get(i).getRealname());
                dataList.add(jcydjbList.get(i).getFsfzh());

                Integer sex = jcydjbList.get(i).getSex();
                if (sex != null) {
                    if (sex == 1) {
                        dataList.add("男");
                    }else if (sex == 2) {
                        dataList.add("女");
                    }else {
                        dataList.add("未知");
                    }
                }else {
                    dataList.add("未知");
                }

                dataList.add(jcydjbList.get(i).getFgzdw());

                dataList.add(lb);

                dataList.add(jcydjbList.get(i).getPhone());

                dataList.add("");
                dataList.add("");

                resultList.add(dataList);
            }

            String title = lb+"-检查员";
            String rootUrl = localFileUploadUtils.getTempFileRootUrl();
            File outFile = ExcelUtils.getLocalDempXlsExcel(resultList, title, rootUrl);

            files.add(outFile);
            fileNames.add(title+".xls");
        }

        PdfUtils.downZip(files,fileNames,response);
    }


    /**
     * 同步用户工作经历
     */
    public void autoUserGzjl(String jcydjbid,String userid,String realname){
        if (StringUtils.isBlank(jcydjbid)){
            return;
        }

        List<SpcyRsJcydjgzjl> djGzjlList = this.queryJcydjGzjlByDjbId(jcydjbid);

        List<SysUserGzjl> userGzjlList = new ArrayList<>();
        for (int i=0; i<djGzjlList.size(); i++){
            SysUserGzjl newGzjl = new SysUserGzjl();
            BeanUtil.copyProperties(djGzjlList.get(i),newGzjl);
            newGzjl.setId(GetUuIdUtils.ReplaceUuId());
            newGzjl.setFuserid(userid);
            newGzjl.setFusermc(realname);

            if (StringUtils.isNotBlank(djGzjlList.get(i).getFgzjssjstr())) {
                newGzjl.setFgzjssj(djGzjlList.get(i).getFgzjssjstr());
            }

            userGzjlList.add(newGzjl);
        }

        if (CollectionUtil.isNotEmpty(userGzjlList)) {
            sysUserGzjlService.saveBatch(userGzjlList);
        }
    }

    /**
     * 同步用户发表文章
     */
    public void autoUserFbwz(String jcydjbid,String userid){
        if (StringUtils.isBlank(jcydjbid)){
            return;
        }
        List<SysUserFbwz> userFbwzList = new ArrayList<>();

        List<SpcyRsJcydjfbwz> fbwzList = queryJcydjFbwzByDjbId(jcydjbid);
        for (int i = 0; i < fbwzList.size(); i++){
            SysUserFbwz userFbwz = new SysUserFbwz();
            BeanUtil.copyProperties(fbwzList.get(i), userFbwz);
            userFbwz.setId(GetUuIdUtils.ReplaceUuId());
            userFbwz.setFuserid(userid);
            userFbwz.setCreateTime(new Date());
            userFbwz.setUpdateTime(new Date());
            userFbwzList.add(userFbwz);
        }

        if (CollectionUtil.isNotEmpty(userFbwzList)) {
            sysUserFbwzService.saveBatch(userFbwzList);
        }
    }

    /**
     * 查询所有有效的检查员工作单位列表
     */
    public List<String> queryAllJcyGzdwList(){
        List<String> resultList = new ArrayList<>();

        List<SpcyRsJcydjb> allList = spcyRsJcydjbService.list(new LambdaQueryWrapper<SpcyRsJcydjb>()
                .ne(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_FQ)
                .isNotNull(SpcyRsJcydjb::getFgzdw));

        if (CollectionUtil.isNotEmpty(allList)){
            resultList = allList.stream().map(SpcyRsJcydjb::getFgzdw).distinct().collect(Collectors.toList());
        }

        return resultList;
    }

    /**
     * 审核状态(批量设置工作单位)
     */
    public void batchSetJcyShzt(JcydjbSetGzdwParamBo paramBo){
        if (StringUtils.isBlank(paramBo.getIds())){
            throw new BusinessException("请选择参数！");
        }
        if (StringUtils.isBlank(paramBo.getFgzdw())){
            throw new BusinessException("单位参数为空！");
        }

        List<String> idsList = StringPatternUtils.getListByIds(paramBo.getIds());
        List<SpcyRsJcydjb> jcydjbList = spcyRsJcydjbService.listByIds(idsList);

        if (CollectionUtil.isNotEmpty(jcydjbList)){
            String logMsg = "";
            for (SpcyRsJcydjb jcydjb : jcydjbList){
                logMsg += "姓名["+jcydjb.getRealname()+"];";
                logMsg += "原工作单位["+jcydjb.getFgzdw()+"];";
                logMsg += "新工作单位["+paramBo.getFgzdw()+"]";

                jcydjb.setFgzdw(paramBo.getFgzdw());
                jcydjb.setFshzt(JcydjbConstant.SHZT_YSC);
                spcyRsJcydjbService.updateById(jcydjb);

                spcyLogService.addLogs(LogConstant.LOGS_JCY_JCYCJGL,logMsg,jcydjb.getId(),"批量设置工作单位",SpcyConstant.LOGS_SYS_TYPE_XG);
            }
        }
    }

    /***
     * 数据对比-检查员采集
     */
    public void dataComparison(MultipartFile file, HttpServletResponse response){
        String fileName = file.getOriginalFilename();
        File fileUpload = localFileUploadUtils.uploadDempFile(file,GetUuIdUtils.ReplaceUuId()+fileName);
        ExcelReader excelReader = ExcelUtil.getReader(fileUpload,0);
        List<Map<String,Object>> excelMapList = excelReader.readAll();

        List<List<String>> resultList = new ArrayList<>();
        List<String> titleList = new ArrayList<>();
        List<String> dataList = null;


        int count = 1;
        int titleSize = 0;
        int keyIndex = 0; // 姓名关键字下标

        for (Map.Entry<String, Object> entry : excelMapList.get(0).entrySet()) {
            String key = Optional.ofNullable(entry.getKey()).orElseGet(() -> "");
            titleList.add(key);

            if (StringUtils.isNotBlank(key) && key.equals("姓名")){
                keyIndex = titleSize;
            }

            titleSize++;
        }

        titleSize += 7;

        titleList.add("性别");
        titleList.add("工作单位");

        titleList.add("是否已扫二维码");

        titleList.add("是否重名");

        titleList.add("药品流通/出现次数");
        titleList.add("药品注册生产/出现次数");
        titleList.add("药物临床/出现次数");

        titleList.add("医疗器械生产流通/出现次数");
        titleList.add("医疗器械临床/出现次数");
        titleList.add("化妆品生产/出现次数");


        resultList.add(titleList);


        for(Map<String,Object> excelMap : excelMapList){

            Set<Map.Entry<String, Object>> entries = excelMap.entrySet();

            if (count > 0){

                int forCount = 0;
                String keyRealname = "";
                dataList = new ArrayList<>();

                for (Map.Entry<String, Object> entry : entries) {

                    String value = "";
                    if (entry.getValue() != null){
                        value = StringPatternUtils.trimString(entry.getValue().toString());
                    }
                    dataList.add(value);

                    if (keyIndex == forCount) {
                        keyRealname = value;
                    }

                    forCount++;
                }

                int ypltNum = 0;
                int ypzcscNum = 0;
                int ywlcNum = 0;

                int ylqxscltNum = 0;
                int ylqxlcNum = 0;
                int hzpjcNum = 0;

                String sfcm = "否"; // 是否重名
                String sex = ""; // 性别
                String gzdw = ""; // 工作单位

                if (StringUtils.isNotBlank(keyRealname)) {

                    boolean isExistJcy = false;
                    List<SpcyRsJcydjb> xmList = spcyRsJcydjbService.list(new LambdaQueryWrapper<SpcyRsJcydjb>()
                            .eq(SpcyRsJcydjb::getRealname, keyRealname)
                            .eq(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_YTJ));
                    if (CollectionUtil.isNotEmpty(xmList)){
                        isExistJcy = true;

                        if (xmList.size()>1){
                            sfcm = "是";
                        }

                        if (xmList.get(0).getSex() != null) {
                            if (xmList.get(0).getSex().equals(1)) {
                                sex = "男";
                            }else if (xmList.get(0).getSex().equals(2)){
                                sex = "女";
                            }
                        }
                        gzdw = xmList.get(0).getFgzdw();
                    }

                    if (isExistJcy){
                        dataList.add(sex);
                        dataList.add(gzdw);
                        dataList.add("是");
                    }else {
                        dataList.add(sex);
                        dataList.add(gzdw);
                        dataList.add("否");
                    }

                    if (isExistJcy) {
                        for (SpcyRsJcydjb jcydjb : xmList) {
                            List<SpcyRsJcydjlb> lbList = spcyRsJcydjlbService.list(new LambdaQueryWrapper<SpcyRsJcydjlb>()
                                    .eq(SpcyRsJcydjlb::getFjcydjbid, jcydjb.getId()));
                            if (CollectionUtil.isNotEmpty(lbList)) {
                                for (SpcyRsJcydjlb lb:lbList) {
                                    if (StringUtils.isBlank(lb.getFjcylb())){
                                        continue;
                                    }

                                    if (StringUtils.isBlank(lb.getFxzzt()) || lb.getFxzzt().equals("0")){
                                        continue;
                                    }

                                    if (lb.getFjcylb().equals(JcydjbConstant.LB_YPLT)){
                                        ypltNum++;
                                    }else if (lb.getFjcylb().equals(JcydjbConstant.LB_YPZCSC)){
                                        ypzcscNum++;
                                    }else if (lb.getFjcylb().equals(JcydjbConstant.LB_YPLC)){
                                        ywlcNum++;
                                    }else if (lb.getFjcylb().equals(JcydjbConstant.LB_YLQXSCLT)){
                                        ylqxscltNum++;
                                    }else if (lb.getFjcylb().equals(JcydjbConstant.LB_YLQXLC)){
                                        ylqxlcNum++;
                                    }else if (lb.getFjcylb().equals(JcydjbConstant.LB_HZPJCY)){
                                        hzpjcNum++;
                                    }
                                }
                            }
                        }
                    }
                }else {
                    dataList.add(sex);
                    dataList.add(gzdw);
                    dataList.add("");
                }

                dataList.add(sfcm);

                dataList.add(String.valueOf(ypltNum));
                dataList.add(String.valueOf(ypzcscNum));
                dataList.add(String.valueOf(ywlcNum));

                dataList.add(String.valueOf(ylqxscltNum));
                dataList.add(String.valueOf(ylqxlcNum));
                dataList.add(String.valueOf(hzpjcNum));

                resultList.add(dataList);
            }

            count++;
        }
        excelReader.close();
        fileUpload.delete();

        // 文件名称
        String title = "数据对比";
        ExcelUtils.getLocalTitleExcel(resultList,title,response);
    }

    /**
     * 数据对比-导出对应信息
     * @param file
     * @param response
     */
    public void dataComparisonExportInfo(MultipartFile file, HttpServletResponse response){
        String fileName = file.getOriginalFilename();
        File fileUpload = localFileUploadUtils.uploadDempFile(file,GetUuIdUtils.ReplaceUuId()+fileName);
        ExcelReader excelReader = ExcelUtil.getReader(fileUpload,0);
        List<Map<String,Object>> excelMapList = excelReader.readAll();

        List<List<String>> resultList = new ArrayList<>();
        List<String> titleList = new ArrayList<>();
        List<String> dataList = null;


        int count = 1;
        int titleSize = 0;
        int keyIndex = 0; // 姓名关键字下标

        for (Map.Entry<String, Object> entry : excelMapList.get(0).entrySet()) {
            String key = Optional.ofNullable(entry.getKey()).orElseGet(() -> "");
            titleList.add(key);

            if (StringUtils.isNotBlank(key) && key.equals("姓名")){
                keyIndex = titleSize;
            }

            titleSize++;
        }

        titleList.add("是否重名");

        titleList.add("身份证号");
        titleList.add("性别");

        titleList.add("工作单位及部门");
        titleList.add("工作单位地址");
        titleList.add("从事工作");
        titleList.add("从事两品医械年限");

        titleList.add("手机");
        titleList.add("QQ号码");

        titleList.add("学历");
        titleList.add("职称");
        titleList.add("职务");




        resultList.add(titleList);


        for(Map<String,Object> excelMap : excelMapList){

            Set<Map.Entry<String, Object>> entries = excelMap.entrySet();

            if (count > 0){

                int forCount = 0;
                String keyRealname = "";
                dataList = new ArrayList<>();

                for (Map.Entry<String, Object> entry : entries) {

                    String value = "";
                    if (entry.getValue() != null){
                        value = StringPatternUtils.trimString(entry.getValue().toString());
                    }
                    dataList.add(value);

                    if (keyIndex == forCount) {
                        keyRealname = value;
                    }

                    forCount++;
                }

                String sfzh = "";
                String sex = ""; // 性别

                String gzdw = ""; // 工作单位
                String gzdwdz = "";
                String csgz = "";
                String gznx = "";

                String phone = "";
                String qqhm = "";

                String xl = "";
                String zc = "";
                String zw = "";

                String sfcm = "否"; // 是否重名

                if (StringUtils.isNotBlank(keyRealname)) {

                    List<SpcyRsJcydjb> xmList = spcyRsJcydjbService.list(new LambdaQueryWrapper<SpcyRsJcydjb>()
                            .eq(SpcyRsJcydjb::getRealname, keyRealname)
                            .eq(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_YTJ));
                    if (CollectionUtil.isNotEmpty(xmList)){

                        if (xmList.size()>1){
                            sfcm = "是";
                        }else {

                            sfzh = xmList.get(0).getFsfzh();

                            if (xmList.get(0).getSex() != null) {
                                if (xmList.get(0).getSex().equals(1)) {
                                    sex = "男";
                                } else if (xmList.get(0).getSex().equals(2)) {
                                    sex = "女";
                                }
                            }

                            gzdw = xmList.get(0).getFgzdw();
                            gzdwdz = xmList.get(0).getFgzdwdz();
                            csgz = xmList.get(0).getFcsgz();
                            gznx = xmList.get(0).getFgznx();

                            phone = xmList.get(0).getPhone();
                            qqhm = xmList.get(0).getFqqhm();

                            xl = xmList.get(0).getFxl();
                            zc = xmList.get(0).getFzc();
                            zw = xmList.get(0).getFzw();
                        }
                    }

                }

                dataList.add(sfcm);

                dataList.add(sfzh);
                dataList.add(sex);

                dataList.add(gzdw);
                dataList.add(gzdwdz);
                dataList.add(csgz);
                dataList.add(gznx);

                dataList.add(phone);
                dataList.add(qqhm);

                dataList.add(xl);
                dataList.add(zc);
                dataList.add(zw);



                resultList.add(dataList);
            }

            count++;
        }
        excelReader.close();
        fileUpload.delete();

        // 文件名称
        String title = "数据对比";
        ExcelUtils.getLocalTitleExcel(resultList,title,response);
    }

    /**
     * 数据对比-检查员类别修改
     * @param file
     * @param fileType
     */
    public void dataComparisonLb(MultipartFile file,String fileType){
        String fileName = file.getOriginalFilename();
        File fileUpload = localFileUploadUtils.uploadDempFile(file,GetUuIdUtils.ReplaceUuId()+fileName);
        ExcelReader excelReader = ExcelUtil.getReader(fileUpload,0);
        List<Map<String,Object>> excelMapList = excelReader.readAll();

        int count = 1;

        String gjz = "手机号"; // 关键字
        String bzgjz = "备注"; // 关键字
        String addIndex = "添加";
        String delIndex = "删除";

        for(Map<String,Object> excelMap : excelMapList){
            Set<Map.Entry<String, Object>> entries = excelMap.entrySet();

            if (count > 0){

                String keyRealname = "";
                String bzKey = "";

                for (Map.Entry<String, Object> entry : entries) {

                    String key = Optional.ofNullable(entry.getKey()).orElseGet(() -> "");
                    if (key.equals(gjz)){
                        if (entry.getValue() != null) {
                            keyRealname = StringPatternUtils.trimString(entry.getValue().toString());
                        }
                    }

                    if (key.equals(bzgjz)){
                        if (entry.getValue() != null) {
                            bzKey = StringPatternUtils.trimString(entry.getValue().toString());
                        }
                    }
                }

                if (StringUtils.isNotBlank(keyRealname) && StringUtils.isNotBlank(bzKey)){
                    List<SpcyRsJcydjb> phoneList = spcyRsJcydjbService.list(new LambdaQueryWrapper<SpcyRsJcydjb>()
                            .eq(SpcyRsJcydjb::getPhone, keyRealname)
                            .ne(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_FQ));

                    String jcylbString = "";
                    if (CollectionUtil.isNotEmpty(phoneList) && phoneList.size() == 1){
                        SpcyRsJcydjb updateJcydjb = phoneList.get(0);
                        for (SpcyRsJcydjb jcydjb : phoneList) {
                            List<SpcyRsJcydjlb> lbList = spcyRsJcydjlbService.list(new LambdaQueryWrapper<SpcyRsJcydjlb>()
                                    .eq(SpcyRsJcydjlb::getFjcydjbid, jcydjb.getId()));
                            if (CollectionUtil.isNotEmpty(lbList)) {
                                for (SpcyRsJcydjlb jcydjlb : lbList){

                                    if (fileType.equals(JcydjbConstant.LB_YPLC) && jcydjlb.getFjcylb().equals(JcydjbConstant.LB_YPLC)){
                                        if (bzKey.equals(addIndex)) {
                                            jcydjlb.setFxzzt("1");
                                        }else if (bzKey.equals(delIndex)){
                                            jcydjlb.setFxzzt("0");
                                        }
                                    }

                                    if (fileType.equals(JcydjbConstant.LB_YPZCSC) && jcydjlb.getFjcylb().equals(JcydjbConstant.LB_YPZCSC)){
                                        if (bzKey.equals(addIndex)) {
                                            jcydjlb.setFxzzt("1");
                                        }else if (bzKey.equals(delIndex)){
                                            jcydjlb.setFxzzt("0");
                                        }
                                    }

                                    if (fileType.equals(JcydjbConstant.LB_YPLT) && jcydjlb.getFjcylb().equals(JcydjbConstant.LB_YPLT)){
                                        if (bzKey.equals(addIndex)) {
                                            jcydjlb.setFxzzt("1");
                                        }else if (bzKey.equals(delIndex)){
                                            jcydjlb.setFxzzt("0");
                                        }
                                    }

                                    if (fileType.equals(JcydjbConstant.LB_YLQXSCLT) && jcydjlb.getFjcylb().equals(JcydjbConstant.LB_YLQXSCLT)){
                                        if (bzKey.equals(addIndex)) {
                                            jcydjlb.setFxzzt("1");
                                        }else if (bzKey.equals(delIndex)){
                                            jcydjlb.setFxzzt("0");
                                        }
                                    }

                                    if (fileType.equals(JcydjbConstant.LB_YLQXLC) && jcydjlb.getFjcylb().equals(JcydjbConstant.LB_YLQXLC)){
                                        if (bzKey.equals(addIndex)) {
                                            jcydjlb.setFxzzt("1");
                                        }else if (bzKey.equals(delIndex)){
                                            jcydjlb.setFxzzt("0");
                                        }
                                    }

                                    if (fileType.equals(JcydjbConstant.LB_HZPJCY) && jcydjlb.getFjcylb().equals(JcydjbConstant.LB_HZPJCY)){
                                        if (bzKey.equals(addIndex)) {
                                            jcydjlb.setFxzzt("1");
                                        }else if (bzKey.equals(delIndex)){
                                            jcydjlb.setFxzzt("0");
                                        }
                                    }

                                    if (StringUtils.isNotBlank(jcydjlb.getFxzzt()) && jcydjlb.getFxzzt().equals("1")){
                                        if (StringUtils.isNotBlank(jcylbString)){
                                            jcylbString += "/";
                                        }
                                        jcylbString += jcydjlb.getFjcylb();
                                    }

                                    spcyRsJcydjlbService.updateById(jcydjlb);
                                }
                            }
                        }

                        updateJcydjb.setFjcylb(jcylbString);
                        spcyRsJcydjbService.updateById(updateJcydjb);
                    }
                }

            }

            count++;
        }

    }

    /**
     * 数据对比-工作单位
     */
    public void dataComparisonGzdw(MultipartFile file){
        String fileName = file.getOriginalFilename();
        File fileUpload = localFileUploadUtils.uploadDempFile(file,GetUuIdUtils.ReplaceUuId()+fileName);
        ExcelReader excelReader = ExcelUtil.getReader(fileUpload,0);
        List<Map<String,Object>> excelMapList = excelReader.readAll();

        int count = 1;
        String gjz = "身份证号"; // 关键字
        String bzgjz = "工作单位"; // 关键字

        String dwqc = "单位全称"; // 关键字
        String dwbm = "工作部门"; // 关键字
        String dwlx = "单位类型"; // 关键字
        String ssds = "所属地市"; // 关键字
        String sfyjs = "是否药检所"; // 关键字
        String dwdz = "单位地址"; // 关键字
        String sftxyp = "是否退休延聘"; // 关键字
        String dwgznx = "单位年限"; // 关键字

        for(Map<String,Object> excelMap : excelMapList) {
            Set<Map.Entry<String, Object>> entries = excelMap.entrySet();

            if (count < 1){
                continue;
            }

            String keyValue = "";
            String bzKeyValue = "";

            String dwqcValue = ""; // 关键字
            String dwbmValue = ""; // 关键字
            String dwlxValue = ""; // 关键字
            String ssdsValue = ""; // 关键字
            String sfyjsValue = "否"; // 关键字
            String dwdzValue = ""; // 关键字
            String sftxypValue = "否"; // 关键字
            String dwgznxValue = "0"; // 关键字
            String csjskssj = "";

            for (Map.Entry<String, Object> entry : entries) {

                String key = Optional.ofNullable(entry.getKey()).orElseGet(() -> "");

                if (key.equals(gjz)){
                    if (entry.getValue() != null) {
                        keyValue = StringPatternUtils.trimString(entry.getValue().toString());
                    }
                }
                if (key.equals(bzgjz)){
                    if (entry.getValue() != null) {
                        bzKeyValue = StringPatternUtils.trimString(entry.getValue().toString());
                    }
                }

                // jg
                if (key.equals(dwqc)){
                    if (entry.getValue() != null) {
                        dwqcValue = StringPatternUtils.trimString(entry.getValue().toString());
                    }
                }
                if (key.equals(dwbm)){
                    if (entry.getValue() != null) {
                        dwbmValue = StringPatternUtils.trimString(entry.getValue().toString());
                    }
                }

                if (key.equals(dwlx)){
                    if (entry.getValue() != null) {
                        dwlxValue = StringPatternUtils.trimString(entry.getValue().toString());
                    }
                }
                if (key.equals(ssds)){
                    if (entry.getValue() != null) {
                        ssdsValue = StringPatternUtils.trimString(entry.getValue().toString());
                    }
                }

                if (key.equals(sfyjs)){
                    if (entry.getValue() != null) {
                        sfyjsValue = StringPatternUtils.trimString(entry.getValue().toString());
                    }
                    sfyjsValue = Optional.ofNullable(sfyjsValue).orElseGet(()->"否");
                }

                if (key.equals(sftxyp)){
                    if (entry.getValue() != null) {
                        sftxypValue = StringPatternUtils.trimString(entry.getValue().toString());
                    }
                    sftxypValue = Optional.ofNullable(sftxypValue).orElseGet(()->"否");
                }

                if (key.equals(dwdz)){
                    if (entry.getValue() != null) {
                        dwdzValue = StringPatternUtils.trimString(entry.getValue().toString());
                    }
                }
                if (key.equals(dwgznx)){
                    if (entry.getValue() != null) {
                        dwgznxValue = StringPatternUtils.trimString(entry.getValue().toString());
                    }

                    dwgznxValue = Optional.ofNullable(dwgznxValue).orElseGet(()->"0");

                    int nx = Integer.valueOf(dwgznxValue);
                    if (nx > 0){
                        int oldYear = 2023 - nx;
                        csjskssj = oldYear+"-01-01";
                    }else {
                        csjskssj = "";
                    }
                }
            }

            if (StringUtils.isBlank(keyValue) || StringUtils.isBlank(bzKeyValue)){
                continue;
            }

            List<SpcyRsJcydjb> jcydjbList = spcyRsJcydjbService.list(new LambdaQueryWrapper<SpcyRsJcydjb>()
                    .eq(SpcyRsJcydjb::getFsfzh, keyValue)
                    .eq(SpcyRsJcydjb::getFsjcjzt, JcydjbConstant.SJCJZT_YTJ));
            if (CollectionUtil.isNotEmpty(jcydjbList) && jcydjbList.size() == 1){
                SpcyRsJcydjb jcydjb = jcydjbList.get(0);

                jcydjb.setFshzt(JcydjbConstant.SHZT_YSC);

                jcydjb.setFdwqc(dwqcValue);
                jcydjb.setFdwbm(dwbmValue);

                jcydjb.setFdwdz(dwdzValue);
                jcydjb.setFdwlx(dwlxValue);

                jcydjb.setFssds(ssdsValue);
                jcydjb.setFsfyjs(sfyjsValue);
                jcydjb.setFsftxyp(sftxypValue);

                jcydjb.setFdwgznx(dwgznxValue);
                jcydjb.setFcsjckssj(csjskssj);

                spcyRsJcydjbService.updateById(jcydjb);

                SpcyRsDwk dwk = new SpcyRsDwk();
                dwk.setFsfyjs(sfyjsValue);
                dwk.setFdwqc(dwqcValue);
                dwk.setFdwlx(dwlxValue);
                dwk.setFdwdz(dwdzValue);
                dwk.setFssds(ssdsValue);

                List<SpcyRsDwk> dwList = spcyRsDwkService.selectList(dwk);
                if (CollectionUtil.isEmpty(dwList)){
                    spcyRsDwkService.addDw(dwk);
                }
            }

            count++;
        }
    }

}
